package org.hbgl.geoentity.db.service;

import com.github.pagehelper.PageHelper;
import org.hbgl.geoentity.db.dao.GeAuthMapper;
import org.hbgl.geoentity.db.dao.GeUnitMapper;
import org.hbgl.geoentity.db.dao.GeUnitTempMapper;
import org.hbgl.geoentity.db.dao.GeUserMapper;
import org.hbgl.geoentity.db.domain.*;
import org.hbgl.geoentity.db.util.EnumFile;
import org.hbgl.geoentity.db.vo.NodeVo;
import org.hbgl.geoentity.db.vo.UnitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 代码功能：
 * 开发日期：2022/4/27
 * 作者：蒋海仑
 * 联系方式：
 * 版本更迭说明：
 * 版权信息：河北广联信息技术有限公司
 */
@Service
public class GeUnitService {
    @Resource
    private GeUnitMapper unitMapper;
    @Resource
    private GeUnitTempMapper unitTempMapper;
    @Resource
    private GeUserMapper userMapper;
    @Resource
    private GeAuthMapper authMapper;

    public List<UnitVo> selectUnitList(String area, Integer examState, String keywords, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return unitMapper.selectUnitList(area, examState, keywords);
    }

    public List<UnitVo> selectUnitExamList(String area, String keywords, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return unitMapper.selectUnitExamList(area, keywords);
    }

    public List<UnitVo> selectUnitExamStateList(String area, String keywords, List<Integer> examState, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return unitMapper.selectUnitExamStateList(area, keywords, examState);
    }

    public UnitVo selectUnitById(Integer id) {
        return unitMapper.selectUnitById(id);
    }
    public UnitVo selectUnitTempById(Integer id) {
        return unitTempMapper.selectUnitById(id);
    }

    public GeUnit findById(Integer id) {
        GeUnitExample example = new GeUnitExample();
        example.or().andIdEqualTo(id).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return unitMapper.selectOneByExample(example);
    }

    public List<GeUnit> findByUnitName(String unitName) {
        GeUnitExample example = new GeUnitExample();
        example.or().andUnitNameEqualTo(unitName).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return unitMapper.selectByExample(example);
    }

    public List<GeUnit> findByUnitName(Integer unitId, String unitName) {
        GeUnitExample example = new GeUnitExample();
        example.or().andIdNotEqualTo(unitId).andUnitNameEqualTo(unitName).andStateNotEqualTo(EnumFile.CommonState.Delete);
        return unitMapper.selectByExample(example);
    }

    @Transactional
    public void add(GeUnit unit, GeUser user) {
        // 添加正式表
        unit.setState(EnumFile.CommonState.Normal);
        unit.setExamState(EnumFile.ExamineState.Wait);
        unit.setAddTime(LocalDateTime.now());
        unit.setEditTime(LocalDateTime.now());
        unitMapper.insertSelective(unit);

        GeUnitTemp unitTemp = new GeUnitTemp();
        BeanUtils.copyProperties(unit, unitTemp);
        unitTempMapper.insertSelective(unitTemp);

        // 添加用户
        user.setState(EnumFile.CommonState.Normal);
        user.setAddTime(LocalDateTime.now());
        user.setEditTime(LocalDateTime.now());
        user.setUnitId(unit.getId());
        userMapper.insertSelective(user);
    }

    @Transactional
    public int updateByIdState(GeUnit unit) {
        unit.setEditTime(LocalDateTime.now());
        int i = unitMapper.updateByPrimaryKeySelective(unit);
        GeUnitTemp unitTemp = unitTempMapper.selectByPrimaryKey(unit.getId());
        if (unitTemp != null) { //临时表没有数据，插入临时表
            unitTemp.setState(unit.getState());
            unitTempMapper.updateByPrimaryKeySelective(unitTemp);
        }
        // int i = unitMapper.updateByPrimaryKeySelective(unit);
        if(i>0) {
            GeUser geUser = new GeUser();
            geUser.setState(unit.getState());
            geUser.setUnitId(unit.getId());
            geUser.setEditTime(unit.getEditTime());
            i = userMapper.updateByUnitIdSelective(geUser);
        }
        return i;
    }

    /**
     * 更新单位信息（更新所有字段，需要判断是否需要审核）
     *
     * @param unit 单位信息
     */
    @Transactional
    public int updateByIdSelective(GeUnit unit) {
        unit.setEditTime(LocalDateTime.now());
        unit.setExamState(EnumFile.ExamineState.Wait);
        unitMapper.updateByPrimaryKeySelective(unit);
        GeUnitTemp unitTemp = unitTempMapper.selectByPrimaryKey(unit.getId());
        if (unitTemp == null) { //临时表没有数据，插入临时表
            GeUnit unitLatest = unitMapper.selectByPrimaryKey(unit.getId());
            unitTemp = new GeUnitTemp();
            BeanUtils.copyProperties(unitLatest, unitTemp);
            unitTempMapper.insert(unitTemp);
        }

        // 临时表有数据，更新临时表
        BeanUtils.copyProperties(unit, unitTemp);
        return unitTempMapper.updateByPrimaryKeySelective(unitTemp);


        // int i = unitMapper.updateByPrimaryKeySelective(unit);
        // if(i>0) {
        //     GeUser geUser = new GeUser();
        //     geUser.setState(unit.getState());
        //     geUser.setUnitId(unit.getId());
        //     geUser.setEditTime(unit.getEditTime());
        //     i = userMapper.updateByUnitIdSelective(geUser);
        // }
        // return i;
    }

    @Transactional
    public int examUnit(GeUnit examUnit) {
        GeUnitTemp unitTempLatest = unitTempMapper.selectByPrimaryKey(examUnit.getId());
        unitTempLatest.setExamState(examUnit.getExamState());
        unitTempLatest.setEditTime(LocalDateTime.now());
        GeUnit unit = new GeUnit();
        BeanUtils.copyProperties(unitTempLatest, unit);
        int i = unitMapper.updateByPrimaryKey(unit);
        if(i>0) {
            if(examUnit.getExamState() == EnumFile.ExamineState.Passed || examUnit.getExamState() == EnumFile.ExamineState.NotPassed) {
                i = unitTempMapper.deleteByPrimaryKey(examUnit.getId());
            }else {
                i = unitTempMapper.updateByPrimaryKey(unitTempLatest);
            }
        }
        return i;
    }

    public Long selectUnitCount() {
        GeUnitExample example = new GeUnitExample();
        example.or().andStateEqualTo(EnumFile.CommonState.Normal).andExamStateEqualTo(EnumFile.ExamineState.Passed);
        return unitMapper.countByExample(example);
    }

    public List<NodeVo> selectAssessList(String area, Integer examState, String keywords, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return unitMapper.selectAssessList(area, examState, keywords);
    }

    public int updateAssessStateById(Integer id, Integer state) {
        GeAuth auth = new GeAuth();
        auth.setId(id);
        auth.setState(state);
        auth.setEditTime(LocalDateTime.now());
        return authMapper.updateByPrimaryKeySelective(auth);
    }

    public NodeVo findAssessByAuthId(Integer id) {
        return authMapper.findAssessByAuthId(id);
    }
}
